package com.zx.idc.backend.gui.api.watch;


import com.zx.idc.backend.gui.api.ftp.BaseFtpClientConfig;
import com.zx.idc.backend.gui.api.ftp.BaseFtpOperatorFactory;
import com.zx.idc.backend.gui.api.ftp.FtpOperator;
import com.zx.idc.backend.gui.api.ftp.basic.BasicFtpOperator;
import com.zx.idc.backend.gui.api.ftp.sftp.SftpOperator;
import com.zx.idc.common.spring.SpringBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.PreDestroy;

/**
 * @author rxliuli
 */
@Configuration
@DependsOn("springConfigUtil")
public class NFtpSpringConfig {
    private final Logger log = LoggerFactory.getLogger(NFtpSpringConfig.class);

    @Autowired
    @Qualifier("ftpClientConfig")
    private BaseFtpClientConfig ftpClientConfig;

    /**
     * 暴露sftp操作对象
     *
     * @return
     */
    @Bean
    public FtpOperator ftpOperator() {
        if (ftpClientConfig == null) {
            log.error("Initial Bean sftpOperator failed, basic ftp client config is null!");
            return null;
        }
        return BaseFtpOperatorFactory.getInstance(ftpClientConfig.getType(), ftpClientConfig);
    }

    @PreDestroy
    public void destroy() {
        log.info("ftp connection Pool is shutdown ing....");
        FtpOperator bean = SpringBeanUtil.getBean(FtpOperator.class);
        bean.closePool();
        log.info("ftp connection Pool is shutdown done....");
    }

}
